package cola0502.project.hellospringbootcola0502.service.impl;

import cola0502.project.hellospringbootcola0502.entity.Cola;
import cola0502.project.hellospringbootcola0502.mapper.ColaMapper;
import cola0502.project.hellospringbootcola0502.service.ColaService;
import org.springframework.security.core.userdetails.User;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UsernameNotFoundException;

import javax.annotation.Resource;

//@Service
public class ColaServiceImpl implements ColaService {

    @Resource
    ColaMapper colaMapper;

    @Override
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
        Cola cola = colaMapper.findColaByName(username);
        if (cola == null) throw new UsernameNotFoundException("登录失败,用户" + username + "不存在");
        return User
            .withUsername(cola.getName())
            .password(cola.getPassword())
            .roles("admin")
            .build();
    }
}
